// pages/information/detail.js
var app = getApp();
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    detail: '',
    liked: false,
    contetnt: '',
    StartTime: '',
    EndTime: '',
    comment: '',
    content: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id
    })

    that.detail();
  
  },
  detail() {
    var that = this;
    var id = that.data.id;
    var opid = wx.getStorageSync('user').openid;
		wx.showLoading({
			            title: '加载中',
		          })
    app.core('get', 'get_hot_news_detail/id/' + id + '/open_id/' + opid + '', "", (res => {
      console.log(res)

      var arrs = res.data;
      var content = res.data.content_article_news.content;
      console.log(content);

      WxParse.wxParse('article', 'html', content, that, 5);
      console.log(WxParse)

      var time = that.getLocalTime(arrs.createTime)
      arrs.createTime = time;

      var comment = res.data.zixun_comment;
      that.setData({
        detail: arrs,
        liked: res.hasCollect,
        comment: comment,
        content: content
      })
			wx.hideLoading()
    }))
  }, //获取详情
  getLocalTime(date) {
    // return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
    var date = new Date(date * 1000); //如果date为13位不需要乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return Y + M + D + h + m + s;


  }, // 转换时间戳

  btnliked() {
    var that = this;
    var like = that.data.liked;

    if (like == false) {
      that.liked()
      console.log(like)
    } else if (like == true) {
      that.dislike()
    }
  },


  liked() {
    var that = this;
    var id = that.data.id;
    var like = that.data.liked;
    var opid = wx.getStorageSync('user').openid;

    app.core('get', 'create_user_collect/open_id/' + opid + '/type/2/lid/' + id + '', "", (res => {
      console.log(res)
      wx.showToast({
        title: '收藏成功',
        icon: 'none',
        duration: 1000
      })
      that.setData({
        liked: true
      })
    }))



  }, // 收藏


  dislike() {

    var that = this;
    var id = that.data.id;
    var like = that.data.liked;
    var opid = wx.getStorageSync('user').openid;

    app.core('get', 'cancel_user_collect/open_id/' + opid + '/type/2/lid/' + id + '', "", (res => {
      console.log(res)
      wx.showToast({
        title: '取消收藏',
        icon: 'none',
        duration: 1000
      })
      that.setData({
        liked: false
      })
    }))


  }, // 取消收藏

  priveimg(e) {
    console.log(e)
    var img = e.currentTarget.dataset.img;
    var arr = [];
    arr.push(img)
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: arr // 需要预览的图片http链接列表
    })
  },
  //预览图片
  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  input(e) {
    this.setData({
      contetnt: e.detail.value
    })
  }, //

  submitsss() {

    var that = this;
    var contetnt = that.data.contetnt
    var opid = app.globalData.openid;
    var id = that.data.id;
    console.log(11122, contetnt)
    if (contetnt == '') {
      wx.showToast({
        title: '请输入评论内容',
        duration: 1000,
        icon: 'none'
      })
    } else {
      app.core('post', 'create_zixun_comment/', {
        zixun_id: id,
        open_id: opid,
        user_id: 0,
        comment: contetnt,
        p_comment_id: 0
      }, (res => {
        console.log(res)
          if (res.resultCode == 200){
            wx.showToast({
              title: '评论成功，待审核',
              duration:1000,
              icon:'none'
            })
            
          var obj={
            id:id
          }
          // setTimeout(function(){
          //   that.onLoad(obj)
          // },1000)

          }
      }))

    }
  },
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var timestamp = Date.parse(new Date()); //进入页面的时间戳
    var Stimestamp = timestamp / 1000;
    this.setData({
      StartTime: Stimestamp
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var timestamp = Date.parse(new Date()); //离开页面的的时间戳
    var Etimestamp = timestamp / 1000;
    this.setData({
      EndTime: Etimestamp
    });
    app.core('post', 'create_data_statistics', {
        // data 部分
        'type': 2,
        'visit_time': this.data.EndTime - this.data.StartTime,
        'read_id': this.data.id,
        'open_id': wx.getStorageSync('user').openid
      }, (res) => {
        console.log(res)
      },
      (err) => {
        console.log(err)
      }
    )
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this;
    var id = that.data.id
    var title = that.data.detail.title;　　 // 设置菜单中的转发按钮触发转发事件时的转发内容
		var imgurl = that.data.detail.photoUrl;
    return {
      title: title, // 默认是小程序的名称(可以写slogan等)
       	 path: 'pages/information/detail?id=' + id + '',
					imageUrl: imgurl, //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      　　　　success: function(res) {　　　　　　 // 转发成功之后的回调
        　　　　　　
        if (res.errMsg == 'shareAppMessage:ok') {
          wx.showToast({
            title: '转发成功',
            icon: 'none',
            duration: 1000
          })　　　　　　
        }　　　　
      },
      fail: function() {　　　　　　 // 转发失败之后的回调
        　　　　　　
        if (res.errMsg == 'shareAppMessage:fail cancel') {　　　　　　　　 // 用户取消转发
          wx.showToast({
            title: '已取消转发',
            icon: 'none',
            duration: 1000
          })　　　　　　
        } else if (res.errMsg == 'shareAppMessage:fail') {　　　　　　　　 // 转发失败，其中 detail message 为详细失败信息
          wx.showToast({
            title: '转发失败',
            icon: 'none',
            duration: 1000
          })　　　　　　
        }　　　　
      },
    }
  }
})